package com.jspgou.cms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.ShippingDao;
import com.jspgou.cms.entity.Shipping;

@Repository
public class ShippingDaoImpl extends HibernateBaseDao<Shipping, Long> implements
		ShippingDao {
	@SuppressWarnings("unchecked")
	public List<Shipping> getList(Long webId,Long storeId,Integer count,boolean all, boolean cacheable) {
		Finder f = Finder.create("from Shipping bean where bean.website.id=:webId");
		f.setParam("webId", webId);
		if (!all) {
			f.append(" and bean.disabled=false");
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId or bean.store.id is null");
			f.setParam("storeId", storeId);
		}else{
			f.append(" and bean.store.id is null");
		}
		if(count!=null){
			f.setMaxResults(count);
		}
		f.append(" order by bean.priority");
		f.setCacheable(cacheable);
		return find(f);
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<Shipping> getList(Long webId,Long storeId,Integer count, boolean cacheable) {
		Finder f = Finder.create("from Shipping bean where bean.website.id=:webId");
		f.setParam("webId", webId);
		f.append(" and bean.disabled=false");
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}else{
			f.append(" and bean.store.id is null");
		}
		if(count!=null){
			f.setMaxResults(count);
		}
		f.append(" order by bean.priority");
		f.setCacheable(cacheable);
		return find(f);
	}
	
	public Pagination getPage(Long webId,Long storeId,Boolean disabled,
			int pageNo, int pageSize){
		Finder f=Finder.create("from Shipping bean where 1=1 ");
		if(webId!=null){
			f.append(" and bean.website.id=:webId");
			f.setParam("webId", webId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}else{
			f.append(" and bean.store.id is null");
		}
		if(disabled!=null){
			f.append(" and bean.disabled=:disabled");
			f.setParam("disabled", disabled);
		}
		f.append(" order by bean.priority ");
		return find(f,pageNo,pageSize);
	}

	public Shipping findById(Long id) {
		Shipping entity = get(id);
		return entity;
	}

	public Shipping save(Shipping bean) {
		getSession().save(bean);
		return bean;
	}

	public Shipping deleteById(Long id) {
		Shipping entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	protected Class<Shipping> getEntityClass() {
		return Shipping.class;
	}
}